

/*
Filename: A6_MergeVoting.do
Goal: Merge institutional holdings and proxy voting data with firm characteristics to create a comprehensive dataset for regression analysis.
Contact: mjha@gsu.edu (author) and gormley@wustl.edu

Data Sources
1. Institutional Holdings (Generated)
File: HoldingFromCRSP.dta
Source: Created by code in A1_CreateHolding
Description: Institutional equity and bond holdings data from CRSP
Purpose: Capture institutional ownership stakes in firms subject to proxy votes

2. Voting Records (Generated)
File: VA_AgainstISS.dta
Source: Created by code in A3_CreateVoting
Description: Institutional voting decisions with ISS recommendations
Purpose: Measure institutional voting behavior, particularly dissent from ISS guidance

3. Financial Ratios (Proprietary)
File: wrds_finratio.dta
Source Path: WRDS → Financial Ratios → Financial Ratios Firm Level by WRDS
Access: Requires WRDS subscription
Purpose: Control for firm's debt to equity ratio in regression models

4. Default Probability (Generated)
File: edf_permco.dta
Source: Created by code in A5_DefaultProb
Description: Expected default frequency (EDF) measures for firms
Purpose: Control for firm credit risk and financial distress
*/


capture log close
capture restore
set more off
clear all

global time = "month"
global institution = "parent_name"
global firm = "permco" 


***** Create holding data
use "HoldingFromCRSP.dta", clear   
replace $time = $time + 1
save "CrspA_$firm.dta", replace
replace $time = $time + 1
save "CrspB_$firm.dta", replace 
replace $time = $time + 1
save "CrspC_$firm.dta", replace

************************** Merge with voting data
use "VA_AgainstISS.dta", clear
merge m:1 $institution $time $firm using "CrspA_$firm" 
drop if _merge == 2
drop _merge
foreach var of varlist tna*{
	rename `var' temp_`var'
}
foreach var of varlist mv*{
	rename `var' temp_`var'
}

merge m:1 $institution $time $firm using "CrspB_$firm" 
drop if _merge == 2
drop _merge
foreach var of varlist tna*{
	replace temp_`var' = `var' if missing(temp_`var')
	drop `var'
}
foreach var of varlist mv*{
	replace temp_`var' = `var' if missing(temp_`var')
	drop `var'
} 

merge m:1 $institution $time $firm using "CrspC_$firm"
drop if _merge == 2
drop _merge
foreach var of varlist tna*{
	replace temp_`var' = `var' if missing(temp_`var')
	drop `var'
	rename temp_`var' `var'
}
foreach var of varlist mv*{
	replace temp_`var' = `var' if missing(temp_`var')
	drop `var'
	rename temp_`var' `var'
}

drop if missing(tna) 
keep if month > 575 

// unique
gsort proposal_id $institution
quietly by proposal_id $institution:  gen dup = cond(_N==1,0,_n)
count if dup > 1
drop dup


**** create regression variables
gen double inv_total = mv_total/tna
gen double inv_bond = mv_bond/tna
gen double inv_equity = (mv_total - mv_bond)/tna
gen double inv_bond_bf = mv_bond_bfi/tna 
gen double inv_bond_ef = (mv_bond - mv_bond_bfi)/tna
gen double inv_bond_ef_ac = (mv_bond - mv_bond_bfi - mv_bond_efi_pa)/tna
gen double inv_bond_ef_pa = mv_bond_efi_pa/tna 
gen bondper = mv_bond/mv_total

* square root 
gen inv_bond_sqrt = sqrt(inv_bond)
gen inv_equity_sqrt = sqrt(inv_equity)

* winsorize
foreach var in inv_total inv_bond inv_equity inv_bond_bf inv_bond_ef inv_bond_ef_ac inv_bond_ef_pa { 
	winsor2 `var', cuts(1 99) replace
}  

* standardize
drop if missing(AGSTISS_CT_SH)
summarize inv_bond
foreach var of varlist inv*{
	egen double std_`var' = std(`var')
}


********
egen institution_month = group($institution $time)
gen date = dofm(month)
format date %d
gen year = yofd(date)

***** variables for heterogeniety analysis
gen big3 = 0
replace big3 = 1 if parent_name == "VANGUARD" | parent_name == "STATE STREET" | parent_name == "PNC"

gen mv_equity = mv_total - mv_bond
gsort proposal_id mv_equity
bysort proposal_id: egen rank_equity = rank(mv_equity), field
gen topholder = 0
replace topholder = 1 if rank_equity < 6 
replace topholder = 0 if mv_equity == 0

merge m:1 permco month using "wrds_finratio_pseudo.dta" 
drop if _merge == 2
summarize de_ratio, detail 
gen de_dummy = 0
replace de_dummy = 1 if de_ratio > 1.709346
replace de_dummy = . if missing(de_ratio)
drop _merge


merge m:1 permco year using "edf_$firm.dta"
drop if _merge==2
drop _merge
save "RegVoting.dta", replace

